<template>
  <div class="search">
    <!-- 位置 -->
    <div class="location bottom-border">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="postion">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>
    <!-- 入住时间范围 -->
    <div class="time-range bottom-border" @click="showCanldar = true">
      <div class="time">
        <div>入住</div>
        <div>{{ startDay }}</div>
      </div>
      <div class="all-time">共{{ diffDays }}晚</div>
      <div class="time">
        <div>离店</div>
        <div>{{ endDay }}</div>
      </div>
    </div>
    <van-calendar
      v-model:show="showCanldar"
      @confirm="onConfirm"
      color="#ff9854"
      type="range"
      :show-confirm="false"
      :round="false"
    />
    <!--  价格时间名宿 -->
    <div class="price-count">
      <div>价格不限</div>
      <div>人数不限</div>
    </div>
    <div class="keyword">关键字/位置/名宿</div>
    <!-- 热门 -->
    <div class="regessets">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="text"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>
    <!-- 搜素 -->
    <div class="search-btn" @click="goSearch">
      <div class="add-search">开始搜素</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import useCityStore from "@/stores/modeules/city";
import { storeToRefs } from "pinia";
import { formatMouthDay, getDiffDays } from "@/utils/format_data";
import useHomeStore from "@/stores/modeules/home";

// 热门建议 -- 数据渲染
const homeStore = useHomeStore();
const { hotSuggests } = storeToRefs(homeStore);

// 页面跳转
const router = useRouter();
const cityClick = () => {
  router.push("/city");
};

// 获取选中的城市数据
const cityStore = useCityStore();
const { currentCity } = storeToRefs(cityStore);

// 时间范围
const nowDate = new Date();
const newDate = new Date();
newDate.setDate(nowDate.getDate() + 1);
const startDay = ref(formatMouthDay(nowDate));
const endDay = ref(formatMouthDay(newDate));
// 计算天数
const diffDays = ref(getDiffDays(nowDate, newDate));
// 时间范围组件
const showCanldar = ref(false);
const onConfirm = (value) => {
  const startSelect = value[0];
  startDay.value = formatMouthDay(startSelect); // 开始时间
  const endSelect = value[1];
  endDay.value = formatMouthDay(endSelect); // 结束时间
  showCanldar.value = false;
  diffDays.value = getDiffDays(startSelect, endSelect); // 选择新时间后重置
};

// 跳转搜素
const goSearch = () => {
  router.push({
    path: "/search",
    query: {
      startDay: startDay.value,
      endDay: endDay.value,
      currentCity: currentCity.value.cityName,
    },
  });
};
</script>

<style scoped lang="scss">
.location {
  display: flex;
  align-items: center;
  padding: 0 20px;
  height: 44px;

  .city {
    flex: 1;
    font-size: 16px;
    color: #333;
  }

  .postion {
    display: flex;
    align-items: center;

    .text {
      font-size: 13px;
      color: #666;
    }

    img {
      margin-left: 5px;
      width: 18px;
    }
  }
}
// 入住时间范围
.time-range {
  display: flex;
  align-items: center;
  padding: 0 35px 10px 20px;
  margin: 10px auto;
  height: 44px;
  justify-content: space-between;
  .time {
    div:nth-child(1) {
      color: #666;
      height: 20px;
    }
  }
  .all-time {
    color: #666;
  }
}

// 价格人数民宿
.price-count {
  display: flex;
  align-items: center;
  padding: 0 35px 0 20px;
  height: 44px;
  justify-content: space-between;
  color: #666;
}
.keyword {
  padding: 0 35px 0 20px;
  color: #666;
  height: 30px;
}

// 热门推荐
.regessets {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 16px;
  font-size: 13px;
  .text {
    padding: 7px 15px;
    margin: 5px;
    border-radius: 18px;
  }
}

// 搜素
.search-btn {
  margin: 0 20px;
  background: orange;
  color: #666;
  margin-top: 15px;
  height: 40px;
  border-radius: 18px;
  .add-search {
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: 40px;
    color: white;
  }
}

// 组件样式更改
.search {
  --van-calendar-popup-height: 100%; // 时间组件高度更改
}
.bottom-border {
  border-bottom: 1px solid #f8f7f6;
}
</style>
